package com.such.kit.calendar.period.impl;

import java.text.ParseException;
import java.util.Calendar;
import com.such.kit.calendar.DateCalculate;
import com.such.kit.calendar.DatePeriodHelper;
import com.such.kit.calendar.period.DatePeriod;
import com.such.kit.string.StringCommons;

public class MonthPeriod extends DatePeriod {

	public DatePeriod add(int amount) throws ParseException {
		return DatePeriodHelper.getInstance(DatePeriodHelper.FREQ_MONTH, 
				super.sfd.format(DateCalculate.add(super.date, amount, DateCalculate.UNIT_MONTH)), super.format);
	}

	public DatePeriod subtract(int amount) throws ParseException {
		return DatePeriodHelper.getInstance(DatePeriodHelper.FREQ_MONTH, 
				super.sfd.format(DateCalculate.subtract(super.date, amount, DateCalculate.UNIT_MONTH)), super.format);
	}

	public String firstFreqDay() {
		String date = StringCommons.merger(super.calendar.get(Calendar.YEAR), "-", super.calendar.get(Calendar.MONTH) + 1, "-01");
		try {
			date = super.sfd.format(super.baseSfd.parse(date));
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		return date;
	}

	public String lastFreqDay() {
		return DateCalculate.subtract(DateCalculate.add(firstFreqDay(), super.format, 1, DateCalculate.UNIT_MONTH), 
				super.format, 1, DateCalculate.UNIT_DAY);
	}

}
